<?php
	class CandidateModel extends Model{
		public function __construct(){
			$this->open(Model::MYSQL);
			$this->calledClass(get_class($this));
		}
		public function getcandidate($cid){
			$this->open(Model::MYSQL);
			$query = "SELECT * FROM candidates WHERE id=$cid";
			$result = parent::query($query);
			$candidate = array();
			while($rows = mysql_fetch_object($result)){
				$candidate[] = $rows;
			}
			return $candidate;
		} 
		public function numRows($result){
			$this->open(Model::MYSQL);
			return is_resource($result)?mysql_num_rows($result):null;
		}
		public function checkCanEmail($mail) {
			$this -> open(Model::MYSQL);
			$query = "select email from Candidates where email='".$mail."'";
			$result = parent::query($query);
			return mysql_num_rows($result) ? true : false;
		}
		public function getUserNameCan($username) {
			$this -> open(Model::MYSQL);
			$query = "SELECT username FROM Candidates where username='".$username."'";
			$result = parent::query($query);
			return mysql_num_rows($result) ? true : false;
		}
		public function checkCanEmailVal($mail) {
			$this -> open(Model::MYSQL);
			$query = "select email from Candidates where email='".$mail."'";
			$result = parent::query($query);
			$row=mysql_fetch_object($result);
			return $row->email;
		}
		public function getRecordSendMail($job_id){
			$this->open(Model::MYSQL);
			$query="SELECT Jobs.title,Jobs.posting_date as start_date,Employers.email FROM Jobs  INNER JOIN Employers  ON Jobs.user_id=Employers.id WHERE Jobs.id=$job_id";
			$result=parent::query($query);
			$record=array();
			$row=mysql_fetch_object($result);;
			return $row;
		}	
		public function getCandidateEmail(){
			$this->open(Model::MYSQL);
			$query = "SELECT * FROM Candidates WHERE id IN (1,2,3,4)";
			$result = parent::query($query);
			$candidate = array();
			while($rows = mysql_fetch_object($result)){
				$candidate[] = $rows;
			}
			return $candidate;
		}
		public function getCanEmail($id){
			$this->open(Model::MYSQL);
			$query = "SELECT email FROM Candidates WHERE id='".$id."'";
			$result = parent::query($query);
			$candidate = array();
			$rows = mysql_fetch_object($result);
				$candidate = $rows;
			
			return $candidate->email;
		}
		public function getValue($table,$field=array(),$where=array()){
			$this->loadModel(Model::MYSQL);
			return parent::getFieldValue($table,$field,$where);
		}
		public function update($fields=array(),$where=array()){
			$this->open(Model::MYSQL);
			return parent::update($fields,$where);
		}
		public function getAlljob($job_id){
			$this->open(Model::MYSQL);
			$query="SELECT Jobs.*,Employers.company_name , Employers.img_id, Employers.firstname, Employers.lastname FROM Jobs, Employers WHERE Employers.id = Jobs.user_id";
			$result=parent::query($query);
			$record=array();
			$row=mysql_fetch_object($result);;
			return $row;
		}
		public function countNonView($where,$table){
			$this->open(Model::MYSQL);
			$query = "SELECT Count(*) as total_records from $table where `$where`=0 ";
			$result = parent::query($query);
			$row=mysql_fetch_object($result);;
			return $row;
		}
		
		//Display Candidate in a day by roth
		public function getCandidattedisplay($page=1,$perpage=20,$start_date="",$end_date=""){
			$this->open(Model::MYSQL);
			$page=$page<1?1:$page;
			$perpage=$perpage<1?20:$perpage;
			$query ="SELECT *  FROM Candidates";
			$query.=" Where DATE_FORMAT(signup_date,'%Y-%m-%d') = '".date("Y-m-d")."' AND username<>'' AND  firstname<>'' AND lastname<>'' AND email<>'' " ;
			$query.=" ORDER BY signup_date DESC ";
			$totalRecords = mysql_num_rows(parent::query($query));
			$totalPages = ceil($totalRecords/$perpage);
			$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
			$result = parent::query($query);
			if($result!==null){
				$rows = $result ;
				$result = array();
				while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
			}
			return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
		}
		
	}
	?>